package ro.msg.office.beans;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Set;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;

import ro.msg.office.ticketmanager.DO.TicketDO;
import ro.msg.office.ticketmanager.back.ifc.TicketManager;
import ro.msg.office.ticketmanager.back.ifc.UserManager;

/**
 * This class is handling the properties from the viewTicketsAdmin.xhtml file 
 * Here are the methods used for generating the table
 * @author Denisa Sulea
 */
public class ViewTicketsAdminBean {
    @EJB
    UserManager userEJB;
    @EJB
    TicketManager ticketEJB;
    
    private HtmlDataTable datatableTickets;
    
    private int nrRows = 0;

    private TicketDO[] ticketsAll = new TicketDO[]{};
	/**
	 * This method replaces the current vector of tickets with a new one
	 * @param ticketsAll - the new vector of tickets
	 */
	public void setTicketsAll(TicketDO[] ticketsAll) {
		this.ticketsAll = ticketsAll; 
	}
	/**
	 * This method loads in a vector of tickets all the required information from the database
	 * @return - all the tickets of the users that are in the same department
	 *  as the administrator that is logged
	 */
	public TicketDO[] getTicketsAll() {
		if(LoginBean.getSigned()){
			ticketsAll = ticketEJB.getTicketsByDepartment(LoginBean.getUser().getDepartment()).toArray(new TicketDO[0]);
			Arrays.sort(ticketsAll,new Comparator<TicketDO>() {
	
				/**
				 * Method used to compare two tickets
				 * @param o1 - first ticket
				 * @param o2 - second ticket
				 * @return - an integer value : 0 if they are equal, 1 if first < second, -1 if first > second
				 */
				public int compare(TicketDO o1, TicketDO o2) {
					Date d1 = o1.getCreationDate();
					Date d2 = o2.getCreationDate();
					if(o2.getStatus()!=o1.getStatus())
						if(o1.getStatus()==1)
							return -1;
						else
							if(o2.getStatus()==1)
								return 1;
							else
								if(d1.after(d2))
									return -1;
								else
									{
									if(d1.before(d2))
										return 1;
									}
					else
						if(d1.after(d2))
							return -1;
						else
							{
							if(d1.before(d2))
								return 1;
							}
					return 0;
				}
			});
		}
		return ticketsAll;
	}
	/**
	 * Method used to set the current dataTable of tickets with a new one
	 * @param datatableTickets - the new dataTable
	 */
	public void setDatatableTickets(HtmlDataTable datatableTickets) {
		this.datatableTickets = datatableTickets;
	}
	/**
	 * Method used to get the current value of the dataTable
	 * @return - the value of the dataTable
	 */
	public HtmlDataTable getDatatableTickets() {
		return datatableTickets;
	}
	/**
	 * Method used to handle the event when a row from the dataTable was selected
	 * @return - a String that represents a navigation between pages : goes to the details of the ticket
	 */
	public String selectTicket() {
		TicketDO ticket = (TicketDO) datatableTickets.getRowData();
		DetailsAdminBean.setTicket(ticket);
		 return "detailsAdmin";
	}
	
	/**
	 * Method used to get the tickets from a department
	 * @return -  a String that represents a navigation between pages : goes to the table with tickets if there is
	 * no error, if there is an error goes to the administrator page
	 */
	public String viewTicketsAdmin(){
		Set<TicketDO> tickets = ticketEJB.getTicketsByDepartment(LoginBean.getUser().getDepartment());
		if(tickets.isEmpty()){
			FacesContext facesContext = FacesContext.getCurrentInstance();
	 		FacesMessage fm = new FacesMessage("There are no tickets!");
	 		facesContext.addMessage(null, fm);
		    return "errorViewTicketsAdmin";
		}
		else
			return "viewTicketsAdmin";
	}
	/**
	 * Method used to set the current number of rows for the dataTable
	 * @param nrRows - the new number
	 */
	public void setNrRows(int nrRows) {
		this.nrRows = nrRows;
	}
	/**
	 * Method used to get the current value of the number of rows for the dataTable
	 * @return - the value of nrRows
	 */
	public int getNrRows() {
		nrRows = ticketsAll.length;
		return nrRows;
	}
}
